package org.apache.sad.core.framework.jaxrpc.handler;

import javax.xml.rpc.handler.MessageContext;

import javax.xml.rpc.handler.soap.SOAPMessageContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.apache.sad.core.framework.jaxrpc.SoapUtil;
import org.apache.sad.core.framework.utils.ObfuscationConfiguration;
import org.apache.sad.core.framework.jaxrpc.handler.BaseSoapHandler;
/**
 * The client handler is used to propagate service context when a service is
 * calling another service via webservice interface.
 */
public class ClientHandler extends BaseSoapHandler {
	private static Log logger = LogFactory.getLog(ClientHandler.class);
	
	@Override
    public boolean handleRequest(MessageContext msgContext) {
    	
		addServiceContextToHeader(msgContext, REQUEST_HEADER);
    	addWsaHeader(msgContext);
       	return true;
    }

	protected String prettyLogSoapMessage(MessageContext context, String text) {
    	StringBuilder logMessage = new StringBuilder();
    	
    	logMessage.append("\n");
    	logMessage.append(text);
    	logMessage.append(":");
    	logMessage.append("\n");
    	logMessage.append("SOAP Msg:\n\n");
    	String soapMessage = SoapUtil.soapMessageToObfuscatedXml((SOAPMessageContext)context, ObfuscationConfiguration.getObfuscatedFieldsAsArray());
    	logMessage.append(soapMessage);
    	
    	return logMessage.toString();
	}    
}